package it.smlm.playbylocation;
import java.io.FileDescriptor;
import java.io.IOException;

import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationListener;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;


public class PlayByLocationListener implements LocationListener {

	private static final String TAG = "PlayByLocationListener";
//	private static final String[] MP3_PATH = {"/sdcard/Back to the future.mp3", "/sdcard/Lo chiamavano Trinita.mp3"};
	private int index_mp3 = 0;
	private PlayByLocation pbl;
	private PlayerManager pm = null;
	
	
	public void setPlayByLocationListener(PlayByLocation pbl) {
		this.pbl = pbl;
	}
	
	@Override
	public void onLocationChanged(Location arg0) {
		Log.i(TAG, "onLocationChanged: " + arg0.toString());
		
		if(pm == null)
			pm = PlayerManager.getPlayerManager();
		
		AssetFileDescriptor afd = null;
		switch(index_mp3) {
			case 0:
				afd = pbl.getResources().openRawResourceFd(R.raw.back_to_the_future);
				index_mp3++;
				break;
			case 1:
				afd = pbl.getResources().openRawResourceFd(R.raw.lo_chiamavano_trinita);
				index_mp3 = 0;
				break;
		}
		
		pm.resetPlayer();
		pm.setDataSource(afd);
		pm.startPlayer();
	}

	@Override
	public void onProviderDisabled(String arg0) {
		Log.i(TAG, "onProviderDisabled: " + arg0.toString());
	}

	@Override
	public void onProviderEnabled(String arg0) {
		Log.i(TAG, "onProviderEnabled: " + arg0.toString());
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		Log.i(TAG, "onStatusChanged: " + arg0.toString());
	}

}
